package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.UserQuestionLike;

public class UserQuestionLikeDao implements BaseDao {

	private final String ADD_USER_QUESTION_LIKE = "INSERT INTO user_question_like (user_id,question_id,direction) VALUES (?,?,?)";
	private final String REMOVE_USER_QUESTION_LIKE = "DELETE FROM user_question_like WHERE id = ?";
	private final String GET_USER_QUESTION_LIKE_BY_ID = "SELECT * FROM user_question_like WHERE id = ?";
	private final String UPDATE_USER_QUESTION_LIKE = "UPDATE user_question_like SET user_id=?, question_id=? WHERE id = ?";
	private final String GET_USER_QUESTION = "SELECT * FROM user_question_like WHERE user_id=? AND question_id=?";
	private final String GET_LIKE_QUESTION_BY_USER_ID =  "SELECT * FROM user_question_like WHERE user_question_like.direction = true AND user_id = ?";
	private static final Logger LOG = Logger.getLogger("stdout");
	
	@Override
	public <T> void create(T element) {
		UserQuestionLike temp = (UserQuestionLike) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_USER_QUESTION_LIKE)) {
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getQuestionId());
			preparedStatement.setBoolean(3, temp.getDirection());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		UserQuestionLike questionLike = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_USER_QUESTION_LIKE_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				questionLike = Converter
						.<UserQuestionLike> fromResaultSetToObject(rs,
								UserQuestionLike.class);
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questionLike;
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_USER_QUESTION_LIKE)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		UserQuestionLike temp = (UserQuestionLike) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_USER_QUESTION_LIKE)) {
			preparedStatement.setInt(3, temp.getId());
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getQuestionId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T getUserQuestion(Integer userId, Integer questionId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		UserQuestionLike userQuestionLike = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_USER_QUESTION)) {
			statement.setInt(1, userId);
			statement.setInt(2, questionId);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				userQuestionLike = Converter
						.<UserQuestionLike> fromResaultSetToObject(rs,
								UserQuestionLike.class);
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) userQuestionLike;

	}

	@SuppressWarnings("unchecked")
	public <T> T getLikeUserQuestion(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<UserQuestionLike> questions = new ArrayList<UserQuestionLike>();
		try (PreparedStatement statement = connection
				.prepareStatement(GET_LIKE_QUESTION_BY_USER_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				questions.add(Converter
						.<UserQuestionLike> fromResaultSetToObject(rs,
								UserQuestionLike.class));
			}
			

		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

}
